//
//  HomeStatusService.m
//  SinaWebo
//
//  Created by Pink Elephant on 15/4/15.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "HomeStatusService.h"
#import "HttpUtil.h"
#import "MJExtension.h"
#import <SDWebImageManager.h>
#import "HomeStatusCacheTool.h"
#import "Status.h"
#import "Photo.h"
#import "TextType.h"
#import "EmotionTool.h"
#import "UserPerfenceUtil.h"
#import "NSRegularExpression+Extension.h"

@implementation HomeStatusService

#pragma mark 获取首页数据
+(void)getHomeStatusDataWith:(HomeRequest *)request success:(void (^)(HomeResponse *))success failure:(void (^)(NSError *))failure{
    // 1.先从缓存里面加载
    NSArray *statusArray = [HomeStatusCacheTool statuesWithParam:request];
    if (statusArray.count) { // 有缓存
        if (success) {
            HomeResponse *result = [[HomeResponse alloc] init];
            result.statuses = statusArray;
            success(result);
        }
    }else{
        NSString *url = [NSString stringWithFormat:@"https://api.weibo.cn/2/statuses/unread_friends_timeline?gsid=%@&wm=3333_2001&i=1a457e4&b=1&from=1066193010&c=iphone&networktype=wifi&v_p=39&skin=default&v_f=1&s=392e5974&lang=zh_CN&sflag=1&ua=iPhone7,2__weibo__6.6.1__iphone__os10.1.1&aid=01AgH4biSzavO8zWu1XN6M6x64NBiQfziSpq_H5pqgj0DT-GE.", [UserPerfenceUtil account].gsid];
        [HttpUtil postWithURL:url params:request.mj_keyValues success:^(NSDictionary *responseObject) {
            if (success) {
                HomeResponse *result=[HomeResponse mj_objectWithKeyValues:responseObject];
                success(result);
                [HomeStatusCacheTool addStatuses:result.statuses];
            }
        } failure:^(NSError *error) {
            if (failure) {
                failure(error);
            }
        }];
    }
}

#pragma mark 获取首页分组数据
+ (void)getAllGrousDataWith:(AllGroupRequest *)request success:(void (^)(AllGroupResponse *))success failure:(void (^)(NSError *))failure{
    [HttpUtil getWithURL:getAllGroupsURL params:request.mj_keyValues success:^(id responseObject) {
        if (success) {
            AllGroupResponse *data = [AllGroupResponse mj_objectWithKeyValues:responseObject];
            success(data);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
@end
